package com.framework.web.sys.controller;


import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.web.common.mappings.SysUrlMappings;
import com.framework.web.sys.domain.Log;
import com.framework.web.sys.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 日志信息管理——控制层
 *
 * @author fusf
 */
@Controller
public class LogController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Spring注入资源信息模块service
     */
    @Autowired
    private LogService logService;


    /**
     * 获取信息列表
     *
     * @param dgm
     * @param log
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_LOG_LIST, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(DataGridModel dgm, Log log, HttpServletRequest request) throws Exception {
        PageRequest pr = DataGridModel.getPageRequest(dgm);
        Page<Log> page = logService.getPageList(log, pr);
        Map<String, Object> map =  PageUtil.getPageMap(page);
        return  map;
    }

    @RequestMapping(value = SysUrlMappings.SYS_LOG_TOSAVEANDUPDATE, method = RequestMethod.GET)
    public ModelAndView toSaveAndUpdate(@PathVariable("id") String id)
            throws Exception {
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_LOG_TOSAVEUPDATE);
        Log log = new Log();
        if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
            log = logService.findById(id);
        }
        modelAndView.addObject("log", log);
        return modelAndView;
    }


}